#include "GnApplicationPCH.h"
#include "GnAppWindow.h"


gtchar GnAppWindow::msWindowClassName[MAX_CLASSNAME] = GnText("Genie Application");


GnAppWindow::GnAppWindow(const gtchar* captionName, guint menuID, guint width, guint height,
	gulong windowStyle) : mMenuID(menuID), mWindowWidth(width) , mWindowHeight(height),
	mWindowStyle(windowStyle), mIsFullScreen(false)
{
	if( captionName && captionName[0] )
	{
		size_t nameLength = GnTstrlen(captionName)+1;
		mCaptionName = GnAlloc(gtchar, nameLength);
		GnTstrcpy(mCaptionName, nameLength, captionName);
	}
	else
	{
		unsigned int uiLen = GnTstrlen(msWindowClassName) + 1;
		mCaptionName = GnAlloc(gtchar, uiLen);
		GnTstrcpy(mCaptionName, uiLen, msWindowClassName);
	}
}

GnAppWindow::~GnAppWindow(void)
{
	GnFree(mCaptionName);
}

gchar* GnAppWindow::GetWindowClassName()
{
	return msWindowClassName;
}
bool GnAppWindow::SetWindowClassName(gtchar* windowName)
{
	size_t strLength = GnTstrlen(windowName);
	if( strLength >= MAX_CLASSNAME )
	{
		GnAssert(strLength < MAX_CLASSNAME );
		return false;
	}
	GnTstrcpy(msWindowClassName, strLength, windowName);
	return true;
}

bool GnAppWindow::CreateGnWindow(int nCmdShow, GnInstance instance, GnWindowHandle parentHandle)
{
	GnWindowHandle wnd = NULL;
	// require the renderer window to have the specified client area
	RECT kRect = { 0, 0, GetWidth(), GetHeight() };
	AdjustWindowRect(&kRect, GetWindowStyle(), GetMenuID());

	unsigned int uiFlags = WS_CHILD;
	if (IsFullScreen())
	{
		wnd = CreateWindow(GetWindowClassName(), GetWindowCaption(), WS_POPUP, 0, 0,
			kRect.right - kRect.left,  kRect.bottom - kRect.top, parentHandle, NULL, instance, NULL);
	}
	else
	{
		uiFlags |= WS_VISIBLE;
		wnd = CreateWindow(GetWindowClassName(), GetWindowCaption(), GetWindowStyle(), 0, 0,
			kRect.right - kRect.left, kRect.bottom - kRect.top, parentHandle, NULL, instance, NULL);
	}
	mMainWindowHandle = wnd;

	// create status window (if requested)
	//if(GetNumStatusPanes())
	//	StatusWindowCreate();

	// Create render window
	mRendererWindowHandle = CreateWindow(GetWindowClassName(), NULL, uiFlags, 0, 0,
		GetWidth(), GetHeight(), wnd, NULL, instance, NULL);

	ShowWindow(GetMainWindowHandle(), nCmdShow);
	UpdateWindow(GetMainWindowHandle());
	return true;
}